<?php 
namespace app\Index\Controller;

use app\index\controller\Base;

Class Article extends Base
{
	/**
     * 初始化方法
     * @author lzh
     */
	public function index()	{
		$id 		= input('id');
		$article 	= db('article')->find($id);
		//
        //$commands 	= $this->command($article['keywords'],$article['id']);
        //统计访问量
       db('article')->where('id', '=', $id)->setInc('click');
        $cates = db('cate')->find($article['cateid']);
        //推荐文章
        $recres=db('article')->where(array('cateid'=>$cates['id'],'status'=>1))->limit(8)->select();
        $this->assign(array(
            'article'  => $article,
            'cates'     => $cates,
            'recres'    => $recres,
            //'commands'  => $commands
            ));
        return $this->fetch('article/index');
    }
    /**
     * 关联函数
     * @return [string] [description]
     */

    public function command($keywords, $v) {
    	$arr = explode(',' , $keywords);
    	static $commands = array();
    	foreach ($arr as $k=>$v)	{
    		$map['keywords'] = ['like','%'.$v.'%'];
            $map['id'] 	= ['neq',$id];
            $artres		= db('article')->where($map)->order('id desc')->limit(8)->select();
            $commands 	= array_merge($commands, $artres);
    	}
    	
    	if  ($commands)	{
            foreach ($commands as $k => $v) { 
                $v = implode(',', $v);  //分割->字符串
                $temp = $v; 
    	    }
            if ($temp) {
                $temp = array_unique($temp); //移除重复值
                foreach ($temp as $k => $v ) {
                $temp[$k]=explode(',', $v);
                }
                return $temp;
            }
        }
    }
}